package com.qipay.pay.impl;

import com.qipay.baas.model.AlipayTransferLog;
import com.qipay.baas.model.AlipayTransferLogExample;
import com.qipay.baas.model.Order;
import com.qipay.core.help.DicHelper;
import com.qipay.core.utils.SpringContextUtil;
import com.qipay.mapper.AlipayTransferLogMapper;
import com.qipay.order.service.OrderService;
import com.qipay.pay.AlipayTransferService;
import com.qipay.pay.entity.CpPayAlipayTransferRequest;
import com.qipay.sp.excutor.ExcutorFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AlipayTransferServiceImpl implements AlipayTransferService
{

    protected final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    OrderService orderService;

    @Autowired
    AlipayTransferLogMapper alipayTransferLogMapper;

    @Override
    public Map<String, Object> transfer(CpPayAlipayTransferRequest entity) throws Exception {
        Order order = orderService.getByTradeNo(entity.getTradeNo());
        AlipayTransferLogExample example = new AlipayTransferLogExample();
        example.or().andTradeNoEqualTo(entity.getTradeNo());
        List<AlipayTransferLog> alipayTransferLogs = alipayTransferLogMapper.selectByExample(example);
        AlipayTransferLog alipayTransferLog = null;
        if(alipayTransferLogs==null||alipayTransferLogs.size()==0){
            alipayTransferLog = new AlipayTransferLog();
            BeanUtils.copyProperties(entity,alipayTransferLog);
            alipayTransferLog.setStatus((byte)0);
            alipayTransferLog.setCreateTime(new Date());
            alipayTransferLog.setTransferNo(orderService.createOrder(18));
            alipayTransferLog.setCollectionAlipay(entity.getZfbNo());
            alipayTransferLogMapper.insert(alipayTransferLog);
        }else{
            alipayTransferLog = alipayTransferLogs.get(0);
            if(alipayTransferLog.getStatus()==1){
                Map<String, Object> result = new HashMap<>();
                result.put("result_code", "0");
                return result;
            }
        }
        Map<String,Object>extendOrderParams = new HashMap<>();
        extendOrderParams.put("alipayTransferLog",alipayTransferLog);
        Map<String, Object> result = excuteFactoryExcutor(order,extendOrderParams);
        return result;
    }



    public Map<String, Object> excuteFactoryExcutor(Order order,Map<String,Object>extendOrderParams) throws Exception {
        Map<String, Object> result = null;
        try {
            ExcutorFactory excutorFactory =
                    (ExcutorFactory) SpringContextUtil.getBean(Class.forName("com.qipay.sp.excutor.alipay.AlipayFactory"));
            result = excutorFactory.createAlipayTransferExcutor().excutor(order,extendOrderParams);
        }catch (Exception ex) {
            ex.printStackTrace();
            logger.error(order.getTradeNo()
                    + " 单笔转账-- 执行工厂方法出错:" + ex.toString());
        }
        return result;
    }



}
